********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************

****
** Open ANES Cumulative File
****

set more off

* use "anes_timeseries_cdf.dta"

********************************************************************************

****
** Data cleaning and recoding
****

* Create ID and year identifiers
gen caseid = VCF0006
gen year = VCF0004
gen sample = 1


* Survey weights
gen weight = VCF0009x


* Survey mode
gen svymode = VCF0017
drop if svymode == 4
	
	
* Self ideology (recoded to range -3-3; -2 Havent thought, -8 DK, -9 NA)
gen ideo = VCF0803
replace ideo = . if ideo < 1 
replace ideo = . if ideo == 9 
recode ideo (1=-3) (2=-2) (3=-1) (4=0) (5=1) (6=2) (7=3)

gen conserv = 1 if ideo > 0 & ideo < 4
replace conserv = 0 if ideo < 0


* Strength of ideological predisposition
gen ideostrength = 0 if ideo == 0
replace ideostrength = 1 if ideo == -1
replace ideostrength = 1 if ideo == 1
replace ideostrength = 2 if ideo == -2
replace ideostrength = 2 if ideo == 2
replace ideostrength = 3 if ideo == -3
replace ideostrength = 3 if ideo == -3


* Self party ID (recoded to range -3-3; -2 DK NA)
gen pid = VCF0301 
replace pid = . if pid == 0
recode pid (1=-3) (2=-2) (3=-1) (4=0) (5=1) (6=2) (7=3)

gen rep = 1 if pid > 0 & pid < 4
replace rep = 0 if pid < 0


* Strength of ideological predisposition
gen pidstrength = 0 if pid == 0
replace pidstrength = 1 if pid == -1
replace pidstrength = 1 if pid == 1
replace pidstrength = 2 if pid == -2
replace pidstrength = 2 if pid == 2
replace pidstrength = 3 if pid == -3
replace pidstrength = 3 if pid == -3


* Education (ranges from 1-7)
gen edu = VCF0140a
replace edu = . if edu >= 8
label define edulab 1 "8 grades or less" 2 "9-12 grades" 3 "High school" ///
	4 "HS + non-academic training" 5 "Some college" 6 "BA" 7 "Advanced"
label values edu edulab


* Race 
gen race = VCF0105a
replace race = . if race == 9

gen black = 1 if race == 2
replace black = 0 if race != 2 & race != .

gen hispanic = 1 if race == 5
replace hispanic = 0 if race != 5 & race != .


* Gender (1=female)
gen gender = VCF0104
replace gender = . if gender < 1
gen female = 1 if gender == 2
recode female (.=0)
label define genderlab 0 "Male" 1 "Female"
label values female genderlab


* Age (number of years) 
gen age = VCF0101
replace age = . if age == 00


* Region
gen south = .
replace south = 0 if VCF0112 == 1
replace south = 0 if VCF0112 == 2
replace south = 0 if VCF0112 == 4
replace south = 1 if VCF0112 == 3
label var south "South Region Dummy"
label define southern 0 "0 Non-South" 1 "1 South"
label values south southern


* Income
gen income = VCF0114 - 1
replace income = . if income < 0


* Religiosity
gen religiosity = VCF0130
recode religiosity (0=.) (7=.) (8=.) (9=.) (5=0) (4=1) (3=2) (2=3) (1=4)

 
* Number of party likes/dislikes (0 - 5 likes)
gen demlike = VCF0314
replace demlike = . if demlike == 9

gen demdislike = VCF0315
replace demdislike = . if demdislike == 9

gen replike = VCF0318
replace replike = . if replike == 9

gen repdislike = VCF0319
replace repdislike = . if repdislike == 9


* Feeling thermometers
gen liberaltherm = VCF0211
replace liberaltherm = . if liberaltherm >= 98
gen conservtherm = VCF0212
replace conservtherm = . if conservtherm >= 98
gen ideothermdiff = abs(liberaltherm - conservtherm)


gen dempartytherm = VCF0218
replace dempartytherm = . if dempartytherm >= 98
gen reppartytherm = VCF0224
replace reppartytherm = . if reppartytherm >= 98
gen partydifftherm = abs(dempartytherm - reppartytherm)


gen demcandtherm = VCF0424
replace demcandtherm = . if demcandtherm >= 98
gen repcandtherm = VCF0426
replace repcandtherm = . if repcandtherm >= 98
gen diffcandtherm = abs(demcandtherm - repcandtherm)


* Affective polarization
alpha ideothermdiff partydifftherm diffcandtherm, gen(affectpol)


* Party placement on ideological scale
gen demideo = VCF0503
replace demideo = . if demideo == 0
replace demideo = . if demideo == 8

gen repideo = VCF0504
replace repideo = . if repideo == 0
replace repideo = . if repideo == 8

gen partyideodiff = abs(demideo - repideo)


* Candidate placement on ideological scale
gen dcandideo = VCF9088
replace dcandideo = . if dcandideo == 0
replace dcandideo = . if dcandideo >= 8

gen rcandideo = VCF9089
replace rcandideo = . if rcandideo == 0
replace rcandideo = . if rcandideo >= 8

gen candideodiff = abs(dcandideo - rcandideo)


* Sorting
gen sorting = abs(ideo - (-1*pid)) * (ideostrength * pidstrength)


* Placement on guaranteed jobs scale 
gen selfjobs = VCF0809
replace selfjobs = . if selfjobs == 0
replace selfjobs = . if selfjobs >= 8
label values selfjobs jobslab

gen demjobs = VCF0513
replace demjobs = . if demjobs == 0
replace demjobs = . if demjobs >= 8
label values demjobs jobslab

gen repjobs = VCF0514
replace repjobs = . if repjobs == 0
replace repjobs = . if repjobs >= 8
label values repjobs jobslab

gen pdiffjobs = abs(demjobs - repjobs)


* Self placement on aid to blacks scale 
gen selfaid = VCF0830
replace selfaid = . if selfaid == 0
replace selfaid = . if selfaid >= 8
label values selfaid aidlab


* Party placement on aid to blacks scale 
gen demaid = VCF0517
replace demaid = . if demaid == 0
replace demaid = . if demaid >= 8
label values demaid aidlab

gen repaid = VCF0518
replace repaid = . if repaid == 0
replace repaid = . if repaid >= 8
label values repaid aidlab

gen pdiffaid = abs(demaid - repaid)


* Self placement on government services scale 
gen selfservice = VCF0839
replace selfservice = . if selfservice == 0
replace selfservice = . if selfservice >= 8
recode selfservice (1=7) (2=6) (3=5) (4=4) (5=3) (6=2) (7=1)


* Party placement on government services scale 
gen demservice = VCF0541
replace demservice = . if demservice == 0
replace demservice = . if demservice >= 8
recode demservice (1=7) (2=6) (3=5) (4=4) (5=3) (6=2) (7=1)

gen repservice = VCF0542
replace repservice = . if repservice == 0
replace repservice = . if repservice >= 8
recode repservice (1=7) (2=6) (3=5) (4=4) (5=3) (6=2) (7=1)

gen pdiffservice = abs(demservice - repservice)


* Self placement on defense spending scale 
gen selfdefense = VCF0843
replace selfdefense = . if selfdefense == 0
replace selfdefense = . if selfdefense >= 8


* Party placement on defense spending scale 
gen demdefense = VCF0549
replace demdefense = . if demdefense == 0
replace demdefense = . if demdefense >= 8

gen repdefense = VCF0550
replace repdefense = . if repdefense == 0
replace repdefense = . if repdefense >= 8

gen pdiffdefense = abs(demdefense - repdefense)


* Self placement on government health insurance scale
gen selfinsure = VCF0806
replace selfinsure = . if selfinsure == 0
replace selfinsure = . if selfinsure == 9


* Party placement on government health insurance scale
gen deminsure = VCF0508
replace deminsure = . if deminsure == 0
replace deminsure = . if deminsure >= 8

gen repinsure = VCF0509
replace repinsure = . if repinsure == 0
replace repinsure = . if repinsure >= 8

gen pdiffinsure = abs(deminsure - repinsure)


* Perceived polarization
alpha pdiffinsure pdiffdefense pdiffservice pdiffaid ///
	pdiffjobs partyideodiff, gen(perceivepol)

	
* Issue extremity
gen issex1 = abs(selfdefense - 4)
gen issex2 = abs(selfservice - 4)	
gen issex3 = abs(selfaid - 4)	
gen issex4 = abs(selfinsure - 4)	
gen issex5 = abs(selfjobs - 4)	
	
alpha issex1-issex5, gen(issextreme)	
	

* Self placement on abortion scale
gen selfabort = VCF0806
replace selfabort = . if selfabort < 1
replace selfabort = . if selfabort > 4
label define abortlab 1 "Never be permitted" 4 "Always permitted"
label values selfabort abortlab


* Do whatever is necessary for equal opportunity
* Note: This variable is reverse coded so that higher values indicate
* more conservative attitudes
gen equalopp = VCF9013 - 1
replace equalopp = . if equalopp > 4
label define equalopportunity 0 "0 Agree strongly" 1 "1 Agree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Disagree somewhat" ///
	4 "4 Disagree strongly"
label values equalopp equalopportunity


* Have gone too far pushing equal rights
* Note: This variable is reverse coded so that higher values indicate
* more conservative attitudes
gen equalrights = VCF9014
replace equalrights = . if equalrights > 5
recode equalrights (5=0) (4=1) (3=2) (2=3) (1=4)
label define equalrightspush 0 "0 Disagree strongly" 1 "1 Disagree somewhat" /// 
	2 "2 Neither agree nor disagree" 3 "3 Agree somewhat" 4 "4 Agree strongly"
label values equalrights equalrightspush


* Big problem is not giving everyone an equal chance*
gen equalchance = VCF9015 - 1
replace equalchance = . if equalchance > 4
label define equalchances 0 "0 Agree strongly" 1 "1 Agree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Disagree somewhat" ///
	4 "4 Disagree strongly"
label values equalchance equalchances


* Better off if we worried less about equality
* Note: This variable is reverse coded so that higher values indicate
* more conservative attitudes
gen lessequal = VCF9017
replace lessequal = . if lessequal > 5
recode lessequal (5=0) (4=1) (3=2) (2=3) (1=4)
label define lessequality 0 "0 Disagree strongly" 1 "1 Disagree somewhat" /// 
	2 "2 Neither agree nor disagree" 3 "3 Agree somewhat" 4 "4 Agree strongly"
label values lessequal lessequality


* Not that big of a problem if people have more of a chance
gen unequal = VCF9016
replace unequal = . if unequal > 5
recode unequal (5=0) (4=1) (3=2) (2=3) (1=4)
label define unequalchance 0 "0 Disagree strongly" 1 "1 Disagree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Agree somewhat" 4 "4 Agree strongly"
label values unequal unequalchance


* Many fewer problems if people were treated equally
gen fewer = VCF9018 - 1
replace fewer = . if fewer > 4
label define fewerproblems 0 "0 Agree strongly" 1 "1 Agree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Disagree somewhat" ///
	4 "4 Disagree strongly"
label values fewer fewerproblems


* Adjusting views of moral behavior
gen changing = VCF0852 - 1
replace changing = . if changing > 4
label define changingmorals 0 "Agree strongly" 1 "Agree somewhat" ///
	2 "Neither agree nor disagree" 3 "Disagree somewhat" 4 "Disagree strongly"
label values changing changingmorals
 
 
* Newer lifestyles contributing to a breakdown in society
* Note: This variable is reverse coded so that higher values indicate*
* more conservative attitudes
gen lifestyles = VCF0851
replace lifestyles = . if lifestyles > 5
recode lifestyles (5=0) (4=1) (3=2) (2=3) (1=4)
label define lifestylesnew 0 "0 Disagree strongly" 1 "1 Disagree somewhat" /// 
	2 "2 Neither agree nor disagree"  3 "3 Agree somewhat" 4 "4 Agree strongly"
label values lifestyles lifestylesnew


* Tolerant of people who choose to live according to their own moral standards
gen standards = VCF0854 - 1
replace standards = . if standards > 4
label define standardsown 0 "0 Agree strongly" 1 "1 Agree somewhat" ///
	2 "2 Neither agree nor disagree" 3 "3 Disagree somewhat" ///
	4 "4 Disagree strongly"
label values standards standardsown
 
 
* More emphasis on traditional family ties*
* Note: This variable is reverse coded so that higher values indicate
* more conservative attitudes
gen family = VCF0853
replace family = . if family > 5
recode family (5=0) (4=1) (3=2) (2=3) (1=4)
label define familyties 0 "0 Disagree strongly" 1 "1 Disagree somewhat" /// 
	2 "2 Neither agree nor disagree"  3 "3 Agree somewhat" 4 "4 Agree strongly"
label values family familyties


* Creating values scales
alpha equalopp equalrights equalchance lessequal unequal fewer changing ///
	lifestyles standards family, detail item generate(values) casewise

alpha equalopp equalrights equalchance lessequal unequal fewer, ///
	detail item generate(equality) casewise

alpha changing lifestyles standards family, detail item ///
	generate(morality) casewise
	
gen valuediff = equality - morality	
gen absvaluediff = abs(valuediff)


* Value "polarization", or extremity
gen valuepold = .
gen valuepolr = .	
	
levelsof year, local(lev)	
	
foreach i of local lev {
sum values if values != . & rep == 1 & year == `i', meanonly
replace valuepold = values - r(mean) if values != . & rep == 0 & year == `i'
sum values if values != . & rep == 0 & year == `i', meanonly
replace valuepolr = values - r(mean) if values != . & rep == 1 & year == `i'
}

replace valuepold = abs(valuepold)
replace valuepolr = abs(valuepolr)
egen valuepol = rowmax(valuepold valuepolr)


* Vote choice
gen prezvote = VCF0704
replace prezvote = . if prezvote == 0
label define prezvotelab 1 "Democrat" 2 "Republican" 3 "Third party"
label values prez prezvotelab

gen votedum = prezvote - 1
replace votedum = . if votedum == 2

gen yesvote = VCF0702 - 1
replace yesvote = . if yesvote < 0 | yesvote > 1

gen congvote = VCF0707
replace congvote = . if congvote == 0
label define votelab 1 "Democrat" 2 "Republican"
label values congvote votelab

gen senvote = VCF0708
replace senvote = . if senvote == 0
label values senvote votelab


* Level of information, as assessed by interviewer
gen info = VCF0050a
replace info = . if info == 9
recode info (1=5) (2=4) (3=3) (4=2) (5=1)
label define infolab 1 "Very low" 2 "Fairly Low" 3 "Average" ///
	4 "Fairly High" 5 "Very High"
label values info infolab


* Interest in campaigns (1 Hardly at all - 4 Most of the time)
gen interest = VCF0310
replace interest = . if VCF0310 == 0
replace interest = . if VCF0310 == 9
label var interest "Interest in the the Campaigns"
label define interestlab 1 "Not much interested" ///
	2 "Somewhat interested" 3 "Very much interested"
label values interest interestlab


* External efficacy index (0 Least efficacious - 100 Most efficacious)
gen efficacy = VCF0648
replace efficacy = . if efficacy == 999


* Trust in government index (0 Least trusting - 100 Most trusting)
gen trust = VCF0656
replace trust = . if trust == 999


* Participation index (1 Lowest - 6 Highest)
gen participate = VCF0723
replace participate = . if participate == 0


* Media exposure index (1 No media - 5 All four media)
gen media = VCF0728
replace media = . if media == 0


* Sophistication scale
factor info interest participate, factor(1) ipf
predict sophscale1

factor interest participate if year == 1978, factor(1) ipf
predict sophscale2

factor interest participate if year == 1982, factor(1) ipf
predict sophscale3

factor interest participate if year == 1990, factor(1) ipf
predict sophscale4

factor interest participate if year == 1994, factor(1) ipf
predict sophscale5

egen sophistication = rowmax(sophscale*)


* Elite polarization
gen housepol = 0.492 if year == 1972
replace housepol = 0.499 if year == 1976
replace housepol = 0.489 if year == 1978
replace housepol = 0.514 if year == 1980
replace housepol = 0.540 if year == 1982
replace housepol = 0.573 if year == 1984
replace housepol = 0.618 if year == 1988
replace housepol = 0.634 if year == 1990
replace housepol = 0.664 if year == 1992
replace housepol = 0.727 if year == 1994
replace housepol = 0.818 if year == 1996
replace housepol = 0.877 if year == 2000
replace housepol = 0.942 if year == 2004
replace housepol = 1.091 if year == 2012


keep caseid-housepol


* Recode variables to range 0-1

foreach v of var ideostrength pidstrength demlike-repdislike ///
ideothermdiff partydifftherm diffcandtherm valuepol perceivepol ///
edu age sophistication income housepol pid ideo sorting ///
trust efficacy participate interest affectpol issextreme{ 
	su `v', meanonly 
	gen `v'2 = (`v' - r(min))/(r(max) - r(min)) 
}

********************************************************************************

*** Cumulative file analyses ***

****
** Figure 1 (saved in "tertiles, prez years.csv")
****

* Generate tertiles
drop if year < 1972
xtile atertile = affectpol2, nquantiles(3)
xtile ptertile = perceivepol2, nquantiles(3)

* Affective by perceived, Figure 1
bysort year: sum affectpol2 if ptertile == 1
bysort year: sum affectpol2 if ptertile == 2
bysort year: sum affectpol2 if ptertile == 3

* Standardized coefficients
bysort ptertile: reg affectpol2 c.year, beta

* Perceived by affective, Figure 1
bysort year: sum perceivepol2 if atertile == 1
bysort year: sum perceivepol2 if atertile == 2
bysort year: sum perceivepol2 if atertile == 3

* Standardized coefficients
bysort atertile: reg perceivepol2 year, beta

****
** Figure 2 (saved in "polarization by info.dta")
****

* Generate knowledge variable
gen know3 = 1 if info < 3
replace know3 = 2 if info == 3
replace know3 = 3 if info > 3 & info < .
drop if know3 == .

* Collapse data
bysort year know3: sum affectpol2

collapse affectpol2 perceivepol2, by(year know3)

bysort know3: corr affectpol2 perceivepol2
